package team.ttorder.ttorder.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import team.ttorder.ttorder.model.User;

@WebFilter(filterName = "BusinessLoginFilter", urlPatterns = {"/tiantiandiancan", "/food/manageFoods", "/food/manageFoodTypes", "/table/manageTables", "/order/manageOrders"})
public class BusinessLoginFilter implements Filter {

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)servletRequest;
		HttpServletResponse response = (HttpServletResponse)servletResponse;
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		if (user != null && user.getRole() == 0) {
			chain.doFilter(request, servletResponse);
			//如果进入判断，则必须要结束该方法，否则网页会报错
			return;
		}
		response.sendRedirect("/adminLogin");
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}
	
}
